class WelcomeController < ApplicationController
  before_filter :login_required

  def index

    # don't know why I need this since there is a before_filter
    redirect_to :controller=>"/account", :action =>"login" and return if session['user'] == nil
    
    session[:selected_project] = '0'
    @user = User.find(session['user'])
    pids = @user.active_projects_ids.join(',')

    # inbox
    @inbox = @user.inbox_tasks

    # without project
    @without_project = @user.without_project
    
    # I had some tasks that had a project different from their parent task....
    #sql = ActiveRecord::Base.connection();
	  #sql.execute "update tasks A set project_id=(select * from (select project_id from tasks T where T.id=task_id) tmp) where task_id!=0";
    #@lost = Task.find_by_sql("select * from tasks where task_id!=0 and project_id!=(select project_id from tasks T where T.id=tasks.task_id)")

    # invitations
    @invit = Invitation.find_all_by_email(@user.email)
    if(@invit != [] and @user.projects.size==0) # accept automatically
      @user.accept_invitation(@invit)
      @invit = []
      flash[:notice] = "You've just joined the projects you've been invited to. Create a new task now by clicking on tasks, in the menu above."
    end

    # view
    @projects        = @user.active_projects
   
    # tasks done
    cond = "done='1'"
    inc = [:project, :user]
    order = 'done_on desc'
    if pids != ''
      cond += " and project_id in (#{pids})" 
      @tasksdone1      = Task.find(:all, :conditions=>cond+" and user_id!=#{@user.id} and done_on > '"+(Date.today-1).to_s + "'", :include=>inc, :order=>order)
      @tasksdone2      = Task.find(:all, :conditions=>cond+" and user_id!=#{@user.id} and done_on between '"+(Date.today-8).to_s+"' and '"+(Date.today-1).to_s + "'", :include=>inc ,  :order=>order)
    else # user has no project, so we can not have tasks done by others
      @tasksdone1      = []
      @tasksdone2      = []
    end  
    @our_tasksdone1    = Task.find(:all, :conditions=>cond+" and user_id=#{@user.id} and done_on > '"+(Date.today-1).to_s + "'", :include=>inc, :order=>order)
    @our_tasksdone2    = Task.find(:all, :conditions=>cond+" and user_id=#{@user.id} and done_on between '"+(Date.today-8).to_s+"' and '"+(Date.today-1).to_s + "'", :include=>inc ,  :order=>order)


    cond  = "done='0' and user_id='#{@user.id}'"
    cond += " and duedate <= '" + (Date.today+8).to_s + "'"
    cond += " and project_id in (#{pids})" if pids != ''

    @deadlines= Task.find(:all, :conditions=>cond, :include=>[:project, :tags, :user], :order=>"duedate,priority,`order`")
    
    calcond = "user_id=#{@user.id}"
    calcond = "project_id in (#{pids})" if pids != ''
    calcond += " and date <= '"+(Date.today+30).to_s + "'"
    calcond += " and date >= '"+(Date.today-2).to_s + "'"
    @calendar = Calendar.find(:all, :conditions=>calcond, :order=>"date")
    
    if pids != ''
      cond = "tasks.done='0' and tasks.project_id in (#{pids}) and notes.updated_on >= '"+(Date.today-21).to_s + "'"
      #, :include=>[:project, :user]
      @notes = Note.find(:all, :select=>"tasks.id as t_id, projects.name as pname, tasks.title, notes.content, notes.updated_on, users.name as uname", :joins=>"JOIN users ON users.id=notes.updated_by JOIN tasks ON tasks.id=notes.task_id JOIN projects ON projects.id=tasks.project_id", :conditions=>cond, :order=>"notes.updated_on desc")
    else
      @notes = []
    end
    
  end

  def send_feedback
    u = User.find(session['user'])
    msg = "From: #{u.name}\nEmail: #{u.email}\n\n#{params['fb']['body']}"
    AppMailer.deliver_alert('Feedback',msg)
    render(:text=>'Msg sent, thank you !')
  end
  
end

